<?php

//各类结算功能控制器
//文件：application\controllers\advice.php

if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Advice extends CI_Controller {

	public $data;
	public $error;

	public function __construct()
	{
		parent::__construct();
		//$this->load->database();
		//载入必要类文件
		$this->load->model('Login_model');
		$this->load->model('User_model');
		$this->load->model('Right_model');
		$this->load->model('Goods_type_model');
		$this->load->model('Advice_model');
		//载入必要库文件
		$this->load->library('pagination');
		$this->load->library('Access_control');
		$this->load->library('Menu_control');
		$this->load->library('Forbid_resubmit');
		//载入配置文件 并 初始化控制器
		$this->data['site'] = $this->config->item('site');
		$this->data['title'] = '各项结算';
		$this->data['styles'] = array('master.css', 'error.css', 'advice.css');
		$this->data['scripts'] = array('master.js', 'advice.js');
		//检查是否是登录用户
		if (!$this->Login_model->check(TRUE))
		{
			header("Location: ".$this->data['site']['root'].'/login');
		}
		//设置站点基本参数
		$this->data['cur_user'] = $this->User_model->getUser($this->input->cookie('user_id'));
		$this->data['header']['username'] = $this->data['cur_user']['realname'];
		$this->data['site']['environment'] = $this->User_model->get_envrionment();
		$this->data['menus'] = $this->config->item('menus');
		$this->data['menus'] = $this->menu_control->main($this->data['menus'], 6);
		$this->data['sub_menus'] = $this->_menus();
		$this->data['sub_menu_index'] = '-1';
	}

	public function __destruct()
	{
		unset($this->data);
		$this->db->close();
	}

	//Privete function in here
	private function _menus()
	{
		return array(
			array(
				'index'	=>	1,
				'url'	=>	$this->data['site']['root'] . 'advice/stock/list',
				'name'	=>	'库存结算单',
				'right'	=> 	'advice.stock'
				),
			array(
				'index'	=>	2,
				'url'	=>	$this->data['site']['root'] . 'advice/sale/list',
				'name'	=>	'销售结算单',
				'right'	=>	'advice.sale'
				),
			array(
				'index'	=> 0,
				),
			array(
				'index'	=> 3,
				'url'	=> 'javascript:void(0)',
				'name'	=> '结算数据',
				'id'	=> 'balance',
				'right'	=> 'advice.make'
				),
			array(
				'index'	=> 0,
				),
			array(
				'index'	=> 3,
				'url'	=> 'javascript:void(0)',
				'name'	=> '检查完整性',
				'id'	=> 'check',
				'right'	=> 'advice.check'
				)
		);
	}

	//Public function is under here
	public function index()
	{
		$this->stock('list');
	}

	public function stock($view_type)
	{

		$this->load->model('Stock_Advices_model');

		if ('list' == $view_type)
		{
			$this->data['sub_menu_index'] = 1;
			$this->data['advices'] = $this->Stock_Advices_model->get_list();
			$this->load->view_('advice_stock_list_view', $this->data);
		}
		else if ('detail' == $view_type)
		{
			$args = func_get_args();
			$this->_view('stock', $args);
		}
		else
		{
			$this->data['error'] = '什么！没有页面！退回去吧～';
			$this->load->view_('error_500_view', $this->data);
		}
	}

	public function sale($view_type)
	{

		$this->load->model('Sale_Advice_model');

		if ('list' == $view_type)
		{
			$this->data['sub_menu_index'] = 2;
			$this->data['advices'] = $this->Sale_Advice_model->get_list();
			$this->load->view_('advice_sale_list_view', $this->data);
		}
		else if ('detail' == $view_type)
		{
			$args = func_get_args();
			$this->_view('sale', $args);
		}
		else
		{
			$this->data['error'] = '什么！没有页面！退回去吧～';
			$this->load->view_('error_500_view', $this->data);
		}
	}

	public function check()
	{
		$report = $this->Advice_model->check_advices();
		if (empty($report))
		{
			$this->data['ajax']['request'] = 'DONE';
			$this->data['ajax']['data'] = '没有发现问题';
		}
		else
		{
			$this->data['ajax']['request'] = 'FAILED';
			$this->data['ajax']['data'] = $report;
		}
		$this->load->view('ajax_json', $this->data);

	}

	private function _view($type)
	{
		$args = func_get_arg(1);
		$this->data['advice'] = $this->Advice_model->view($type, $args);
		if ($this->data['error'] = $this->Advice_model->error)
		{
			$this->load->view_('error_500_view', $this->data);
			return;
		}
		else
		{
			switch ($type) {
				case 'stock':
					$this->load->view_('advice_stock_detail_view', $this->data);
					break;
				case 'sale':
					$this->load->view_('advice_sale_detail_view', $this->data);
					break;
				default:
					$this->error = '什么！没有页面！退回去吧～';
					break;
			}
		}
	}

	public function make()
	{
		$this->data['sub_menu_index'] = 1;
		$this->db->trans_begin();
		$this->Advice_model->make_advices();
		if ($this->Advice_model->error)
		{
			$this->db->trans_rollback();
			$this->data['ajax']['request'] = 'FAILED';
			$this->data['ajax']['data'] = '结算出错！' . $this->Advice_model->error;
			if ('CANNOT_BALANCE_LESS_THEN_0' == $this->Advice_model->error)
			{
				$this->data['ajax']['data'] = '存在负库存，不能结算！';
			}
		}
		else
		{
			$this->data['ajax']['request'] = 'DONE';
			$this->data['ajax']['data'] = '结算完成';
			$this->db->trans_commit();
		}
		$this->load->view('ajax_json', $this->data);
	}

}